home *** CD-ROM | disk | FTP | other *** search
- Path: news.sprintlink.net!datalytics!usenet
- From: Rob Stewart <stew@datalytics.com>
- Newsgroups: comp.lang.c++
- Subject: Re: Why should I use classes....or should I?
- Date: Fri, 29 Mar 1996 13:31:08 -0500
- Organization: Datalytics, Inc
- Message-ID: <315C2C6C.60FF@datalytics.com>
- References: <4jcohu$7c8@artemis.it.luc.edu>
- NNTP-Posting-Host: 204.62.224.71
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (WinNT; I)
-
- Rick Olson wrote:
- >
- > I am an experienced FORTRAN programmer and have a reasonable
- > knowledge of C. Most of my programming is related to my research
- > which is generally related to constructing and comparing heuristic
- > alogorithms for difficult problems. I am about to start a new
- > project and am trying to decide whether C++ is appropriate. I'd
- > appreciate any feedback.
- >
- > Suppose I want to compare 10 algorithms for solving a problem (eg
- > bin packing). With C I would code the routines for each algorithm
- > and then write something to read the data and run it through each
- > algorithm printing the results. From where I sit this is pretty
- > straight forward. All the algorithms require the same input
- > data (perhaps item sizes) and reports the same results (number of
- > bins, solution time...). This seems to be a function driven
- > application rather than a data driven app.
- >
-
- From what I understand of what you describe, the algorithms
- would be normal functions as you would do in C. However, the
- data could be encapsulated nicely in a class. Member functions
- could simplify access by, for example, setting several related
- values at once, validating relationships when you set values,
- etc. The advantage of that is that the functions implementing
- the algorithms can reuse that logic by simply invoking the
- member function on the data object it was given.
-
- The other thing you mentioned was that they need to report the
- results. You can write a function to call from the algorithm
- functions to do this, or you can write a member function to do
- it. That's a little fuzzier because the class may not know the
- entire output required by the various algorithm functions. On
- the other hand, the class would know the data types intimately
- and could produce output appropriate to various parts of the
- data that the algorithm functions could call at appropriate
- times.
-
- The thing to remember about classes--before you even consider
- advanced language features--is the encapsulation they provide.
- This means that you have specified an interface to select pieces
- of data and the functions that may directly interact with that
- data. You can do the same things with C, but C++ gives you a
- language facility that protects you from your mistakes.
-
- Once you declare your data private in a class, only member
- functions may access it. This ensures that you handle that data
- reliably in all cases. Any other functions that wish to use
- that data must do so through the interface you prescribe in the
- class declaration. The compiler will complain if you try to
- access data any way other than specified by the class
- declaration.
-
- > There are a few features of C++ that are more appealing than C.
- > Not needing to pass integer addresses to functions, then dereference
-
- I assume you are referring to passing by reference. Yes, that
- is a nice addition.
-
- > them is one. Right now I don't see any compelling argument for
- > using classes unless it is to learn how they work. In fact, it seems
- > to me that if I use classes I will actually pay a performance penalty
- > because of the overhead associated with binding functions that can't
- > be done at compilation time because of the possibility of polymorphism.
- >
-
- You only incur this overhead when you use virtual functions and
- when calling such functions through a pointer or reference to a
- class. If you use an object directly, the compiler invokes the
- member function of the object's class directly, avoiding the
- virtual function mechanism.
-
- > Am I missing something? Is there a reason why I should be trying to
- > think in terms of objects rather than thinking of C++ as "a better C".
- >
-
- That's a decent reason in and of itself.
-
- > I would appreciate any personal opinions/experience and pointers to
- > good books that may help me understand the *WHY* of OOP for these
- > types programs rather than just the *HOW* of classes.
- >
- > Thanks in advance--
- > Rick Olson
-
- --
- Robert Stewart | My opinions are usually my own.
- Datalytics, Inc. | stew@datalytics.com
-